import subprocess

from libs import time_helper


def get_local_last_ver():
    cmd = 'git log --pretty=format:"%h %cd" -1'
    sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    out, err = sp.communicate()
    ver_hash = ''
    create_at = ''
    for line in out.splitlines():
        ver_info = str(line, 'utf-8')
        ver_info_arr = ver_info.split(' ')
        if len(ver_info_arr) == 7:
            ver_hash = ver_info_arr[0]
            utc_time_str = '{} {} {} {} {} {}'.format(ver_info_arr[1], ver_info_arr[2], ver_info_arr[3], ver_info_arr[4], ver_info_arr[5], ver_info_arr[6])
            ts = time_helper.time_str_to_ts(utc_time_str, '%a %b %d %X %Y %z')
            create_at = time_helper.ts_to_time_str(ts)
    return ver_hash, create_at
